#include"CMain.h"
#include<SnowstormXGame.h>
#include<strsafe.h>

EVENT(enter_pressed)
{
	// check actions paused state
	if(v(actions_paused))
	{
		// unpause them
		XPrintString(L"resuming\n");
		XResume();
	}
	else
	{
		// pause them
		XPrintString(L"pausing\n");
		XPauseAll();
	}
}

EVENT(xbox_a_held)
{
	// print a message
	XPrintString(L"Xbox A held\n");

	// finished
	return;
}

EVENT(xbox_b_pressed)
{
	// print a message
	XPrintString(L"Xbox B pressed\n");
	return;
}

EVENT(xbox_b_released)
{
	// print a message
	XPrintString(L"Xbox B released\n");
	return;
}

EVENT(space_pressed)
{
	// print a message
	XPrintString(L"Space Pressed\n");

	XCreatePersistentEntity(NULL);

	// finished
	return;
}


EVENT(escape_released)
{
	// terminate the engine
	XKill(123456);

	// finished
	return;
}

#define PERSISTENT_ENTITY_COUNT 8000

uint64_t CMain::Action(void *start_params)
{

	// register event for escape key released
	XRegisterKeyPressedEvent(space_pressed, VK_SPACE);
	XRegisterKeyPressedEvent(enter_pressed, VK_RETURN);
	XRegisterKeyReleasedEvent(escape_released, VK_ESCAPE);
	XRegisterKeyPressedEvent(xbox_b_pressed, VK_XBOXB);
	XRegisterKeyHeldEvent(xbox_a_held, VK_XBOXA);
	XRegisterKeyReleasedEvent(xbox_b_released, VK_XBOXB);
	XRegisterCloseClickEvent(escape_released);
	

	PEntTest **ents = new PEntTest*[PERSISTENT_ENTITY_COUNT];
	for(int i = 0; i < PERSISTENT_ENTITY_COUNT; i++)
	{
		ents[i] = new PEntTest();
		ents[i]->SetName(L"Persistent Entity", L"", i, 0);
		ents[i]->MakeExecutionReady((void*)(uint64_t)i);
		XCreatePersistentEntity(ents[i]);
	}


	while(1)
	{
		wchar_t msg[512];
		StringCbPrintf(msg, 512, L"FPS: %f last frame time (s): %.10f\n", 
			ev->current_FPS,
			ev->frame_time);
		XPrintString(msg);

		if(KEY_PRESSED(VK_XBOXSTART))
			break;

		XWait(-1);
	}
	


	// finished
	return 26;
}

uint64_t PEntTest::Action(void *start_params)
{
	uint64_t ent_id = (uint64_t)start_params;
	double wait_time = -1 * ((double)ent_id + 1);

	while(1)
	{
		for(int i = 0; i < 10000; i++){}
		XWait(1);

		/*
		wchar_t msg[512];
		StringCbPrintf(msg, 512, L"ENT%u waiting %f seconds\n", ent_id, wait_time);
		XPrintString(msg);
		XWait(wait_time);*/
		break;
		
	}

	// finished
	return 0;
}
